package com.church.action;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import org.apache.catalina.connector.Request;
import org.apache.struts2.ServletActionContext;

import com.church.db.User;
import com.church.services.UserServices;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Register extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private UserServices userServices;
	private String name;
	private String password;
	private String passwordAgain;
	private boolean sex;
	private Date birthday;
	private String graduated;
	private String address;
	private String mobile;
	private String telephone;
	private String email;
	private String workingOrgans;
	private String workPosition;
	private String artisticSkills;
	private boolean success;
	public UserServices getUserServices() {
		return userServices;
	}

	public void setUserServices(UserServices userServices) {
		this.userServices = userServices;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getPasswordAgain() {
		return passwordAgain;
	}

	public void setPasswordAgain(String passwordAgain) {
		this.passwordAgain = passwordAgain;
	}

 

	public String getGraduated() {
		return graduated;
	}

	public void setGraduated(String graduated) {
		this.graduated = graduated;
	}

	public String getAddress() {
		return address;
	}

	public void setAddress(String address) {
		this.address = address;
	}

	public String getMobile() {
		return mobile;
	}

	public void setMobile(String mobile) {
		this.mobile = mobile;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String getWorkingOrgans() {
		return workingOrgans;
	}

	public void setWorkingOrgans(String workingOrgans) {
		this.workingOrgans = workingOrgans;
	}

	public String getWorkPosition() {
		return workPosition;
	}

	public void setWorkPosition(String workPosition) {
		this.workPosition = workPosition;
	}

	public String getArtisticSkills() {
		return artisticSkills;
	}

	public void setArtisticSkills(String artisticSkills) {
		this.artisticSkills = artisticSkills;
	}




	public boolean isSex() {
		return sex;
	}

	public void setSex(boolean sex) {
		this.sex = sex;
	}

	public String execute() {  
		User exuser = userServices.findUserByName(name);
		if (exuser == null) {
			System.out.println(ServletActionContext.getRequest().getCharacterEncoding());
			User user = new User();
			user.setAddress(address);
			System.out.println("************"+address);
			user.setArtisticSkills(artisticSkills);
			user.setBirthday(birthday);
			Date datetime = new Date(System.currentTimeMillis());
			user.setCreateDate(datetime);
			user.setEmail(email);
			user.setGraduated(graduated);
			user.setMobile(mobile);
			user.setName(name);
			user.setPassword(password);
			user.setSex(sex);
			user.setState("0");
			user.setType("user");
			user.setGroup("user");
			user.setWorkingOrgans(workingOrgans);
			user.setWorkPosition(workPosition);
			success = userServices.register(user).equals("success");
			if(success)
			{
				ActionContext actionContext = ActionContext.getContext();
				Map session = actionContext.getSession();
				session.put("USER_NAME", name);
				session.put("USER_ID", user.getUserid());
			}
			return "success";
		} else {
			success=false;
			return "success";
		}
	}

	public Date getBirthday() {
		return birthday;
	}

	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

}
